<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="/css/admin.css" rel="stylesheet" type="text/css"></link>
<script type="text/javascript" src="/js/jquery.js"> </script>
<title>文章列表 admin page</title>
<style type="text/css">
a.page-nav {
  font-size:14px;
  border:1px solid red;
  margin:2px;
  padding:2px;
}
a.current {
  text-decoration:none;
  border:0;
}
li {
  line-height: 32px;
}
input[type="radio"]:checked+label{color:#a22;}
</style>
</head>
<body>
  <div class="main">
<a name="top"></a> 
<a href="#bottom">底部</a>
    <ul style="margin:0;padding:2px;">
    <?php 
    include("util.php");
    $conn = mysql_connect("127.0.0.1","root","");
    mysql_select_db("xingtan");
    
    $category_map = array();
    $sql=sprintf("SELECT id, name, title FROM categories;");
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)) {
      $category_map[$row['id']] = $row;
    }

    $level_map = array();
    $sql=sprintf("SELECT id, title FROM levels;");
    $result = mysql_query($sql);
    while($row = mysql_fetch_array($result)) {
      $level_map[$row['id']] = $row;
    }
   
    $max_id = 1000000;
    if($_GET['max']) {
      $max_id = intval($_GET['max']);
    }

$count_per_page = 50;
$page = intval($_GET['page']);
if ($page == null || $page < 1) {
  $page = 1;
}
$sql=sprintf("SELECT count(id) AS total FROM articles");
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$total_pages = ceil($row['total'] / $count_per_page);
CommonPageNav($total_pages, $count_per_page, $page, 3, "/article-admin.php?page=%d");

    $sql=sprintf("SELECT id, title, url, category, level FROM articles ORDER BY id DESC LIMIT %d", $page *$count_per_page);

    $result = mysql_query($sql);

    $idx = 0;
    while($row = mysql_fetch_array($result)) {
      if($idx++ < ($page - 1) * $count_per_page) {
        continue;
      }
      print('<li style="margin-top:18px;list-style:none;border:1px solid green;">');
      $meta = GetArticleMeta($row['id']);
      printf('<a id="article-id" target="_blank" href="/a/%d.html">%d</a> &nbsp; <a target="_blank" href="/content-edit.php?id=%d">edit</a>&nbsp; <u>%s</u> <a target="_blank" href="%s">source</a>&nbsp;', $row['id'], $row['id'], $row['id'], $meta['u_name'], $row['url']);
      printf('<input type="text" size="40" value="%s"/>', $row['title']);
      $cat_count = 0;
      foreach ($category_map as $id => $cat) {
        if ($cat_count++ % 3 == 0) {
           print('<br/>');
        }
        if($id == $row['category']) {
          printf('<input type="radio" name="article_cat_%d" value="%d" checked><label>%s</label>', $row['id'], $id, $cat['name']);
        } else {
          printf('<input type="radio" name="article_cat_%d" value="%d"><label>%s</label>', $row['id'], $id, $cat['name']);
        }
      }
      print("<hr/>");

      foreach ($level_map as $id => $level) {
        if($id == $row['level']) {
          printf('<input type="radio" name="article_level_%d" value="%d" checked><label>%s</label>', $row['id'], $id, $level['title']);
        } else {
          printf('<input type="radio" name="article_level_%d" value="%d"><label>%s</label>', $row['id'], $id, $level['title']);
        }
      }

      printf('<hr/><a href="#nogo" class="update-button" id="%d">update</a> <a href="#nogo" class="del-button">delete</a>', $row['id'], $row['id']);
      print('</li>');
    }
    mysql_close($conn);
    ?>
    </ul>
	</div>
<a name="bottom"></a>
<a href="#top">顶部</a>
<script type="text/javascript">
    $('.del-button').click(
      function() {
        if(!confirm ("确定删除此文章?")) {
          return;
        }
        var li = $(this).parent();
        var aid = $('#article-id', li).text();
        $.ajax({
          url: "/article-del.php?id=" + aid,
          context : $(this),
          success : function() {
            $(this).text("删除成功");
          }
        });
      }
    );
    $('.update-button').click(
      function() {
        var li = $(this).parent();
        var aid = $('#article-id', li).text();
        $.ajax({
          url: "/article-update.php?id=" + aid
            + '&t=' + encodeURIComponent($('input:text', li).val()) 
            + '&c=' + $('input[name=article_cat_' + aid + '][checked]"', li).val()
            + '&l=' + $('input[name=article_level_' + aid + '][checked]"', li).val(),
          success : function(rsp_text) {
            alert(rsp_text);
          }, 
          error : function() {
            alert('更新失败');
          }
        });
      }
    );
</script>
</body>
